package com.leyou.auth.web;

import com.leyou.auth.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author mySun
 * @create 2021-07-28 16:58
 */
@RestController
@RequestMapping("/user")
public class UserAuthController {
    @Autowired
    private UserAuthService userAuthService;

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param response 用于写回cookie
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void> userLogin(
            @RequestParam("username")String username,
            @RequestParam("password")String password,
            HttpServletResponse response
    ){
        userAuthService.userLogin(username,password,response);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/logout")
    public ResponseEntity<Void> userLogout(@CookieValue("LY_TOKEN")String token,HttpServletResponse response){
        userAuthService.userLogout(token,response);
        return ResponseEntity.ok().build();
    }

}
